home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_7 / simprl.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  472 b   |  21 lines  |  [MATF/MATL]

  1. function s = simprl(f,a,b,m)
  2. % s = simprl(f,a,b,m)
  3. % Quadrature using Simpson`s rule.
  4. % f is the name of the function, input.
  5. % a is the left  endpoint, input.
  6. % b is the right endpoint, input.
  7. % m is the number of subintervals, input.
  8. % s is the Simpson rule sum, output.
  9. h  = (b - a)/(2*m);
  10. s1 = 0;
  11. s2 = 0;
  12. for k=1:m,
  13.   x = a + h*(2*k-1);
  14.   s1 = s1 + feval(f,x);
  15. end
  16. for k=1:(m-1),
  17.   x = a + h*2*k;
  18.   s2 = s2 + feval(f,x);
  19. end
  20. s = h*(feval(f,a)+feval(f,b)+4*s1+2*s2)/3;
  21.